﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class DonDatKH
    {
        
        public int MaDDH { get; set; }
        public string MaDDHHienThi
        {
            get
            {
                return "DDKH" + MaDDH;
            }
        }
        public int MaKH { get; set; }
        public string TenKH { get; set; }
        public int MaNV { get; set; }
        public string MaNVHienThi
        {
            get
            {
                return "NV" + MaNV;
            }
        }
        public string TenNV { get; set; }
        public DateTime NgayDat { get; set; }
        public double SoTien { get; set; }
        public string GhiChu { get; set; }
        public List<ChiTietDonDatKH> DanhSachChiTietDonDat { get; set; }
        

       
        public DonDatKH()
        {
            this.MaDDH = 0;
            DanhSachChiTietDonDat = new List<ChiTietDonDatKH>();
        }

        public DonDatKH(int maDDH, int maKH, string tenKH, int maNV, string tenNV, DateTime ngayDat, double soTien, string ghiChu)
        {
            this.MaDDH = maDDH;
            this.MaKH = maKH;
            this.TenKH = tenKH;
            this.MaNV = maNV;
            this.TenNV = tenNV;
            this.NgayDat = ngayDat;
            this.SoTien = soTien;
            this.GhiChu = ghiChu;
        }
        

        

        public void ThemMoiChiTietDonDat(int maDDH, int maHang,
            string tenHang, int soLuong, double giaBan, DateTime ngayGiao)
        {
            ChiTietDonDatKH chiTietDonDat = new ChiTietDonDatKH(maDDH,
                maHang, tenHang, soLuong, giaBan, ngayGiao);

            DanhSachChiTietDonDat.Add(chiTietDonDat);
        }

        public void CapNhatChiTietDonDat(int index, int maDDH, int maHang,
            string tenHang, int soLuong, double giaBan, DateTime ngayGiao)
        {
            ChiTietDonDatKH chiTietDonDat = DanhSachChiTietDonDat[index];
            chiTietDonDat.CapNhat(maHang, tenHang, soLuong, giaBan, ngayGiao);
        }

        public void XoaChiTietDonDat(int index)
        {
            DanhSachChiTietDonDat.RemoveAt(index);
        }

        private bool TonTaiChiTietDonDat(int maCTDDH)
        {
            foreach (var chitietDonDat in this.DanhSachChiTietDonDat)
            {
                if (chitietDonDat.MaCTDDH == maCTDDH)
                    return true;
            }
            return false;
        }

        public double TinhTongTienHang()
        {
            double tongTien = 0;
            foreach (var chiTietDonDat in DanhSachChiTietDonDat)
            {
                tongTien =  tongTien + chiTietDonDat.SoLuong * chiTietDonDat.GiaBan;
            }
            return tongTien;
        }

        public static List<DonDatKH> LayDanhSach_NgayGiao(DateTime tungay, DateTime denngay)
        {
            try
            {
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

                return (from p in db.ChiTietKHDats
                        where p.NgayGiao.Date >= tungay.Date && p.NgayGiao.Date <= denngay.Date
                        select new DonDatKH(p.MaDDH, p.DonDatKH.MaKH, p.DonDatKH.KhachHang.TenKH, p.DonDatKH.MaNV, p.DonDatKH.NhanVien.TenNV, p.DonDatKH.NgayDat, p.DonDatKH.SoTien, p.DonDatKH.GhiChu)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }
       
        public static int ThemMoi(DonDatKH nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.DonDatKH tamObject = new DataAccess.DonDatKH();
            tamObject.MaKH = nhapObject.MaKH;

            tamObject.MaNV = nhapObject.MaNV;
            tamObject.NgayDat = nhapObject.NgayDat;

            tamObject.SoTien = nhapObject.TinhTongTienHang();
            tamObject.GhiChu = nhapObject.GhiChu;
            db.DonDatKHs.InsertOnSubmit(tamObject);
            db.SubmitChanges();

            foreach (var chitietDonDat in nhapObject.DanhSachChiTietDonDat)
            {
                DataAccess.ChiTietKHDat chiTietTamObject = new DataAccess.ChiTietKHDat();
                chiTietTamObject.MaDDH = tamObject.MaDDH;
                chiTietTamObject.MaHang = chitietDonDat.MaHang;

                chiTietTamObject.GiaBan = chitietDonDat.GiaBan;
                chiTietTamObject.SoLuong = chitietDonDat.SoLuong;
                chiTietTamObject.NgayGiao = chitietDonDat.NgayGiao;

                db.ChiTietKHDats.InsertOnSubmit(chiTietTamObject);
            }

            db.SubmitChanges();

            return tamObject.MaDDH;
        }

        public static void CapNhat(DonDatKH nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            
            var tamObject = (from p in db.DonDatKHs
                             where p.MaDDH == nhapObject.MaDDH
                             select p).Single();
            tamObject.MaKH = nhapObject.MaKH;
            tamObject.MaNV = nhapObject.MaNV;

            tamObject.NgayDat = nhapObject.NgayDat;

            tamObject.SoTien = nhapObject.TinhTongTienHang();
            tamObject.GhiChu = nhapObject.GhiChu;

            
            foreach (var chiTietTamObject in tamObject.ChiTietKHDats)
            {
                if (!nhapObject.TonTaiChiTietDonDat(chiTietTamObject.MaCTDDH))
                    db.ChiTietKHDats.DeleteOnSubmit(chiTietTamObject);
            }

            
            foreach (var chitietDonDat in nhapObject.DanhSachChiTietDonDat)
            {
                if (chitietDonDat.MaCTDDH == 0)
                {
                    DataAccess.ChiTietKHDat chiTietTamObject = new DataAccess.ChiTietKHDat();
                    chiTietTamObject.MaDDH = nhapObject.MaDDH;

                    chiTietTamObject.MaHang = chitietDonDat.MaHang;
                    chiTietTamObject.GiaBan = chitietDonDat.GiaBan;

                    chiTietTamObject.SoLuong = chitietDonDat.SoLuong;
                    chiTietTamObject.NgayGiao = chitietDonDat.NgayGiao;

                    db.ChiTietKHDats.InsertOnSubmit(chiTietTamObject);
                }
                else
                {
                    var chiTietTamObject = (from p in db.ChiTietKHDats
                                            where p.MaCTDDH == chitietDonDat.MaCTDDH
                                            select p).Single();
                    chiTietTamObject.MaHang = chitietDonDat.MaHang;

                    chiTietTamObject.GiaBan = chitietDonDat.GiaBan;

                    chiTietTamObject.SoLuong = chitietDonDat.SoLuong;
                    chiTietTamObject.NgayGiao = chitietDonDat.NgayGiao;
                }
            }

            db.SubmitChanges();
        }




        public static DonDatKH LayDonDatKH(int maDonDat)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            DonDatKH donDatHang = (from p in db.DonDatKHs
                                   where p.MaDDH == maDonDat
                                   select new DonDatKH(p.MaDDH, p.MaKH, p.KhachHang.TenKH, p.MaNV, p.NhanVien.TenNV, p.NgayDat, p.SoTien, p.GhiChu)).Single();

            List<ChiTietDonDatKH> danhsachChiTietDonDat = (from p in db.ChiTietKHDats
                                                           where p.MaDDH == maDonDat
                                                           select new ChiTietDonDatKH(p.MaCTDDH, p.MaDDH, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.GiaBan, p.NgayGiao)).ToList();


            if (danhsachChiTietDonDat == null || danhsachChiTietDonDat.Count == 0)
                danhsachChiTietDonDat = new List<ChiTietDonDatKH>();

            donDatHang.DanhSachChiTietDonDat = danhsachChiTietDonDat;

            return donDatHang;
        }




        public static void Xoa(int maDDH)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var xoaObject = (from p in db.DonDatKHs
                             where p.MaDDH == maDDH
                             select p).Single();

            db.DonDatKHs.DeleteOnSubmit(xoaObject);

            db.SubmitChanges();
        }

      

        public static List<DonDatKH> LayDanhSach_KH(string maKHHT)
        {
            int maKH = Int32.Parse(maKHHT.ToString().Substring(2));

            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            return (from p in db.DonDatKHs
                    where p.MaKH == maKH

                    select new DonDatKH(p.MaDDH, p.MaKH, p.KhachHang.TenKH, p.MaNV, p.NhanVien.TenNV, p.NgayDat, p.SoTien, p.GhiChu)).ToList();
        }



        public static List<DonDatKH> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            return (from p in db.DonDatKHs
                    select new DonDatKH(p.MaDDH, p.MaKH, p.KhachHang.TenKH, p.MaNV, p.NhanVien.TenNV, p.NgayDat, p.SoTien, p.GhiChu)).ToList();
        }

        public static List<DonDatKH> LayDanhSach_NVDat(string maNVHT)
        {
            try
            {
                int maNV = Int32.Parse(maNVHT.ToString().Substring(2));

                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.DonDatKHs
                        where p.MaNV == maNV

                        select new DonDatKH(p.MaDDH, p.MaKH, p.KhachHang.TenKH, p.MaNV, p.NhanVien.TenNV, p.NgayDat, p.SoTien, p.GhiChu)).ToList();


            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static List<DonDatKH> LayDanhSach_NgayDat(DateTime tungay, DateTime denngay)
        {
            try
            {
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.DonDatKHs
                        where p.NgayDat.Date >= tungay.Date && p.NgayDat.Date <= denngay.Date
                        select new DonDatKH(p.MaDDH, p.MaKH, p.KhachHang.TenKH, p.MaNV, p.NhanVien.TenNV, p.NgayDat, p.SoTien, p.GhiChu)).ToList();

            }
            catch 
            { 
                return LayDanhSach(); 
            }
        }

      
        
    }
}
